<?php
	class Edit_deal extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			if (!isset($_SESSION)) {
			    session_start();
			    session_regenerate_id();
			}
			$this->load->helper("url");
			
		}
		public function index()
		{
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$data['action']='edit_deal';
			$did=0;
			if(isset($_REQUEST['did']))
				$did=$_REQUEST['did'];
			$this->load->model("mdeal");
			$data['deal_detail']=$this->mdeal->get_deal($did);	
			$this->load->view("deal_template", $data);
		}
		
		public function edit_deal_ok()
		{
			$id_deal=trim($_REQUEST['id_deal']);
			$did_category=trim($_REQUEST['did_category']);
			$uploadDir1 = 'upload/deal/'.$did_category.'/';
			$uploadDir = '../upload/deal/'.$did_category.'/';
			$filePath1=trim($_REQUEST['dsmall_image1']);
			$filePath2=trim($_REQUEST['dlarge_image1']);
			if (!file_exists($uploadDir)) mkdir($uploadDir, 0777, true);
			$deal=strtotime('now');
			
			if(isset($_FILES['dsmall_image'])){
				if($_FILES['dsmall_image']['name']!=""){
				$error ="";
			    $maxsize    = 104857600;
			    $acceptable = array(
			        'image/jpeg',
			        'image/jpg',
			        'image/gif',
			    	'image/bmp',
			        'image/png'
			    );
						$fileName1=explode(".",$_FILES['dsmall_image']['name']);
						$tmpName = $_FILES['dsmall_image']['tmp_name'];
						$fileSize = $_FILES['dsmall_image']['size'];
						$fileType = $_FILES['dsmall_image']['type'];
						$fileName="small_".$deal.'.jpg';
						$filePath1 = $uploadDir . $fileName;
				$co=0;
				if(($_FILES['dsmall_image']['size'] >= $maxsize) ) {
			        $error = 'File too large. File must be less than 1 megabytes.'; $co++;
			    }
			
			    if(!in_array($_FILES['dsmall_image']['type'], $acceptable) && !empty($_FILES["dsmall_image"]["type"])) {
			        $error = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; $co++;
			    }
			
			    if($co == 0) {
					if(move_uploaded_file($tmpName, $filePath1)) {
				        echo "success";
					} else{
					       echo '<script>alert("Upload unsuccessfull !!!!");
			            			history.back();		</script>';
					       die();
					}
					
			    } else {
			            echo '<script>alert("'.$error.'");
			            			history.back();		</script>';
			        die(); //Ensure no more processing is done
				}
				$filePath1 = $uploadDir1 . $fileName;
				}
			}
			

			if(isset($_FILES['dlarge_image'])){
				if($_FILES['dlarge_image']['name']!=""){
				$error ="";
			    $maxsize    = 104857600;
			    $acceptable = array(
			        'image/jpeg',
			        'image/jpg',
			        'image/gif',
			    	'image/bmp',
			        'image/png'
			    );
						$fileName1=explode(".",$_FILES['dlarge_image']['name']);
						$tmpName = $_FILES['dlarge_image']['tmp_name'];
						$fileSize = $_FILES['dlarge_image']['size'];
						$fileType = $_FILES['dlarge_image']['type'];
						$fileName="large_".$deal.'.jpg';
						$filePath2 = $uploadDir . $fileName;
				$co=0;
				if(($_FILES['dlarge_image']['size'] >= $maxsize) ) {
			        $error = 'File too large. File must be less than 1 megabytes.'; $co++;
			    }
			
			    if(!in_array($_FILES['dlarge_image']['type'], $acceptable) && !empty($_FILES["dlarge_image"]["type"])) {
			        $error = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; $co++;
			    }
			
			    if($co == 0) {
					if(move_uploaded_file($tmpName, $filePath2)) {
				        echo "success";
					} else{
					       echo '<script>alert("Upload unsuccessfull !!!!");
			            			history.back();		</script>';
					       die();
					}
					
			    } else {
			            echo '<script>alert("'.$error.'");
			            			history.back();		</script>';
			        die(); //Ensure no more processing is done
				}
				$filePath2 = $uploadDir1 . $fileName;
				}
			}
			
			
			
			
			$dname=trim($_REQUEST['dname']);
			$dname = str_replace("'", "\'", ($dname));
			
			$dprice=trim($_REQUEST['dprice']);
			$dprice = str_replace( ',', '', $dprice);
			
			$dsale_off=trim($_REQUEST['dsale_off']);
			$dsale_off=str_replace( ',', '.', $dsale_off);
			
			$dprice_sale_off=trim($_REQUEST['dprice_sale_off']);
			$dprice_sale_off = str_replace( ',', '', $dprice_sale_off);
			
			$dtitle=str_replace("'", "", ($_REQUEST['dtitle']));
			
			$dadvantage=str_replace("'", "", ($_REQUEST['dadvantage']));
			$dcondition=str_replace("'", "", ($_REQUEST['dcondition']));
			$dcontent=str_replace("'", "", ($_REQUEST['dcontent']));
			
			$top_seller=0;
			if(isset($_REQUEST['top_seller']))
			$top_seller=1;
			
			$top_view=0;
			if(isset($_REQUEST['top_view']))
			$top_view=1;
			
			$top_100=0;
			if(isset($_REQUEST['top_100']))
			$top_100=1;
			
			$freeship=0;
			if(isset($_REQUEST['freeship']))
			$freeship=1;
			
			$delivery=trim($_REQUEST['delivery']);
			
			
			$this->load->model("mdeal");
			$this->mdeal->edit_deal($id_deal,$did_category,$dname,$dprice,$dsale_off,$dprice_sale_off,$filePath1,$filePath2,$dtitle,$dadvantage,$dcondition,$dcontent,$top_seller,$top_view,$top_100,$freeship,$delivery);
			
			echo '<script>
						window.location.assign("'.$_SESSION['linkdeal'].'");
						</script>';
			
		}
		public function approve()
		{
			$did=$_REQUEST['did'];
			$status=$_REQUEST['status'];
			$data=array();
			$this->load->model("mdeal");
			$this->mdeal->approve($did,$status);
		}
	}
?>